package jTopaz;

import org.globus.ftp.GridFTPClient;
import java.io.File;
import org.globus.ftp.FileRandomIO;
import java.io.RandomAccessFile;

/**
The actual execution thread for a download transfer.
*/
public class DownloadThread extends Thread {

    private GridFTPClient client;
    private File localFile;
    private String remoteFile;
    private long size;
    private boolean complete;

	/**
	Creates a new DownloadThread object.
	@param client the GridFTPClient object that will do the actual transfer.
	@localFile the file that will be downloaded to.
	@remoteFile the name of the file that will serve as the source.
	@size the size of the remote file.
	*/
    public DownloadThread(GridFTPClient client, File localFile, String remoteFile, long size) {

		this.client = client;
		this.localFile = localFile;
		this.remoteFile = remoteFile;
		this.size = size;
		complete = false;

    }

	/**
	The actual execution.  calls GridFTPClient.extendedGet();
	*/
    public void run() {
	
		try {
		
	    	RandomAccessFile raFile = new RandomAccessFile(localFile, "rw");
	    	FileRandomIO sink = new FileRandomIO(raFile);
	    	MyMarkerListener listener = new MyMarkerListener();
		
	    //I want to remove this, but for now, keep it...
	    //defeats the whole purpose of extendedGet
	    	System.out.println(client);
	    	client.setPassive();
	    	client.setLocalActive();
	    	client.extendedGet(remoteFile, size, sink, listener);
	    	complete = true;
		}
	
		catch (Exception e) {
	    	System.out.println("you die in here??");
	    	System.out.println(e);
	    	e.printStackTrace();
		}
    }
    
    /**
    Returns whether or not the transfer has completed.
    */
    public boolean transferComplete() {
	
	//System.out.println("in here? " + complete);
	return complete;
    }
}
